package com.wind.taobao.api.trade.user.convert
{
	import com.wind.taobao.api.trade.user.model.Location;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * Location转换器
	 * @author fxy
	 */
	public class LocationConvert
	{
		/**
		 * 转换Location集合
		 */
		public static function convertLocations(value:Object, type:String="json"):ArrayCollection
		{
			var locations:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							locations.addItem(convertLocation((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return locations;
		}

		/**
		 * 转换Location
		 */
		public static function convertLocation(value:Object, type:String="json"):Location
		{
			var location:Location=new Location();
			if (value == null)
				return location;
			if (type == TaobaoConstants.XML)
			{
				location=convertXMLToLocation(XML(value), location);
			}
			else
			{
				location=convertJSONToLocation(value, location);
			}
			return location;
		}

		/**
		 * JSON转换为Location
		 */
		private static function convertJSONToLocation(json:Object, location:Location):Location
		{
			if (json.hasOwnProperty(TaobaoConstants.ZIP))
			{
				location.zip=json[TaobaoConstants.ZIP];
			}
			if (json.hasOwnProperty(TaobaoConstants.ADDRESS))
			{
				location.address=json[TaobaoConstants.ADDRESS];
			}
			if (json.hasOwnProperty(TaobaoConstants.CITY))
			{
				location.city=json[TaobaoConstants.CITY];
			}
			if (json.hasOwnProperty(TaobaoConstants.COUNTRY))
			{
				location.country=json[TaobaoConstants.COUNTRY];
			}
			if (json.hasOwnProperty(TaobaoConstants.ADDRESS))
			{
				location.district=json[TaobaoConstants.DISTRICT];
			}
			if (json.hasOwnProperty(TaobaoConstants.STATE))
			{
				location.state=json[TaobaoConstants.STATE];
			}
			return location;
		}

		/**
		 * XML转换为Location
		 */
		private static function convertXMLToLocation(value:XML, location:Location):Location
		{
			return location;
		}

	}
}